package com.nny.alarm;

import com.nny.alarm.R;
import com.nny.alarm.edit.EditFragment;
import com.nny.alarm.menu.MainMenu;
import com.nny.alarm.more.MoreFragment;
import com.umeng.analytics.onlineconfig.UmengOnlineConfigureListener;
import com.umeng.update.UmengUpdateAgent;
import com.uxwine.alarm.Alarm;
import com.uxwine.cmm.BaseFragmentActivity;
import com.uxwine.widget.ActionWindow.OnWindowEventListener;
import com.uxwine.widget.FoldView;
import com.uxwine.widget.SlideView;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class MainActivity extends BaseFragmentActivity implements SlideView.OnSlideListener, OnWindowEventListener
{
	
	FrameLayout mMainView;
	FoldView mCtnMore;
	SlideView mCtnMain;
	Button mbtnShowMain;
	MainMenu mMenu;

	int[] mnStates;
	int mnCurrent;
	MoreFragment mFrmtMore;
	EditFragment mFrmtMain;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mCtnMain = (SlideView) findViewById(R.id.main_ctn_main);
		mCtnMore = (FoldView) findViewById(R.id.main_ctn_more);

		FragmentManager fm = getSupportFragmentManager();
		mFrmtMore = (MoreFragment) fm.findFragmentById(R.id.main_frg_more);
		mFrmtMain = (EditFragment) fm.findFragmentById(R.id.main_frg_main);

		mMenu = new MainMenu(this, findViewById(R.id.main_btn_menu));
		mMenu.setOnWindowEventListener(this);
		mMenu.defaultMenu(this);

		mCtnMain.setBaseView(mCtnMore);
		mCtnMain.setOnSlideListener(this);
		
		UmengUpdateAgent.update(this);
	}
	
	@Override
	protected void onStart()
	{
		super.onStart();		
		GuideActivity.guide(this, GuideActivity.GUIDE_EDIT);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(requestCode == GuideActivity.GUIDE_EDIT)
		{
			if(resultCode == RESULT_CANCELED)
				finish();
		}
	}



	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		switch (keyCode)
		{
		case KeyEvent.KEYCODE_VOLUME_DOWN:

		case KeyEvent.KEYCODE_VOLUME_UP:
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onBackPressed()
	{
		if (mCtnMain.isShow())
		{
			mFrmtMain.stop();
			super.onBackPressed();
		} 
		else
		{
			showEidtor(null);
		}
	}

	public void onClickEvent(View v)
	{
		int id = v.getId();
		if (id == R.id.main_btn_more)
		{
			showMore();			
		}
		else if(id == R.id.main_btn_menu)
		{
			mMenu.toggle();
		}
	}

	public void showEidtor(Alarm a)
	{
		mFrmtMain.edit(a);
		mCtnMain.toggle();
	}

	public void showMore()
	{
		mCtnMain.toggle();
		if(mFrmtMore.loadAlarms())
			GuideActivity.guide(this, GuideActivity.GUIDE_LIST);
	}

	@Override
	public void onSliding(SlideView v, int state)
	{
		if (state == SlideView.VIEW_OUT)
		{
			mFrmtMain.hide();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onMenuOpened(int featureId, Menu menu)
	{
		mMenu.toggle();
		return false;
	}

	@Override
	public void onExit(int code) {
		// TODO Auto-generated method stub
		
	}
}
